/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.http.server.handler.ext;

import hxg.http.server.NamedRequestHandler;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.HttpAsyncExchange;
import org.apache.http.protocol.HttpContext;

/**
 *
 * @author hstel-hxg
 */
public class PostTestHandler extends NamedRequestHandler {

    @Override
    public void handle(HttpRequest t, HttpAsyncExchange hae, HttpContext hc) throws HttpException, IOException {
        System.out.println(t.getClass().getSimpleName());
        if (t instanceof BasicHttpEntityEnclosingRequest) {
            BasicHttpEntityEnclosingRequest request = (BasicHttpEntityEnclosingRequest) t;
            InputStream is = request.getEntity().getContent();
            byte[] bytes = new byte[is.available()];
            is.read(bytes);
            System.out.println(new String(bytes));
        }
        String page = "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
                + "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /></head>"
                + "<body><form action=\"\" method=\"post\">请输入您的微信验证码<br/>"
                + "<input type=\"text\" name=\"code\" placeholder=\"请输入您的微信验证码\" /></br>"
                + "<input name=\"button\" type=\"submit\" value=\"上网\" /></form></body>"
                + "</html>";
        HttpResponse response = hae.getResponse();
        response.setEntity(new NStringEntity(page, ContentType.create("text/html", "UTF-8")));
        hae.submitResponse();
    }

}
